package com.ruoyi.web.publish.story.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.publish.resume.domain.Resume;
import com.ruoyi.web.publish.story.domain.Story;
import com.ruoyi.web.publish.story.mapper.StoryMapper;
import com.ruoyi.web.publish.story.service.IStoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

/**
 * 我的故事Service业务层处理
 *
 * @author houxuzhong
 * @date 2021-08-24
 */
@Service
public class StoryServiceImpl implements IStoryService {
    @Autowired
    private StoryMapper storyMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询我的故事
     *
     * @param id 我的故事主键
     * @return 我的故事
     */
    @Override
    public Object selectStoryById(Long id) {
        return storyMapper.selectStoryById(id);
    }

    /**
     * 查询我自己的故事
     *
     * @return 我的故事
     */
    @Override
    public Story getMyStory() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();

        Story story = new Story();
        story.setCreateById(user.getUserId());
        List<Story> storyList = storyMapper.selectStoryList(story);
        if (CollectionUtils.isEmpty(storyList)) {
            return story;
        } else {
            return storyList.get(0);
        }
    }

    /**
     * 根据id查询我的故事
     *
     * @return 我的故事
     */
    @Override
    public Story getUserStory(Long id) {
        Story story = new Story();
        story.setCreateById(id);
        List<Story> storyList = storyMapper.selectStoryList(story);
        if (CollectionUtils.isEmpty(storyList)) {
            return story;
        } else {
            return storyList.get(0);
        }
    }

    /**
     * 查询我的故事列表
     *
     * @param story 我的故事
     * @return 我的故事
     */
    @Override
    public List<Story> selectStoryList(Story story) {
        return storyMapper.selectStoryList(story);
    }

    /**
     * 新增我的故事
     *
     * @param story 我的故事
     * @return 结果
     */
    @Override
    public int insertStory(Story story) throws UnknownHostException {
        story.setCreateTime(DateUtils.getNowDate());

        story.setUrl(story.getUrl().replace("localhost", getIp()));

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        story.setCreateById(user.getUserId());
        story.setCreateBy(user.getUserName());
        return storyMapper.insertStory(story);
    }

    /**
     * 修改我的故事
     *
     * @param story 我的故事
     * @return 结果
     */
    @Override
    public int updateStory(Story story) throws UnknownHostException {
        story.setUpdateTime(DateUtils.getNowDate());

        story.setUrl(story.getUrl().replace("localhost", getIp()));

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        story.setCreateById(user.getUserId());
        story.setCreateBy(user.getUserName());
        return storyMapper.updateStory(story);
    }

    /**
     * 批量删除我的故事
     *
     * @param ids 需要删除的我的故事主键
     * @return 结果
     */
    @Override
    public int deleteStoryByIds(Long[] ids)
    {
        return storyMapper.deleteStoryByIds(ids);
    }

    /**
     * 删除我的故事信息
     *
     * @param id 我的故事主键
     * @return 结果
     */
    @Override
    public int deleteStoryById(Long id) {
        return storyMapper.deleteStoryById(id);
    }

    private String getIp() throws UnknownHostException {
        InetAddress ip4 = Inet4Address.getLocalHost();
        return ip4.getHostAddress();
    }
}
